<?php


class tx_zlib_viewhelper_image extends tx_zlib_abstract_viewhelper
{

    /**
	 * Use absolute URL for the image (eg. http://www.mysite.com/image.jpg)
	 *
	 * @var boolean
     */
    private $useAbsoluteUrl = false;

    /**
     * @param string $path
     * @param integer|string $width
     * @param integer|string $height
     * @param array $htmlAttribs
     * @return tx_zlib_viewhelper_image|string
     */
	public function image($path = '', $width = 0, $height = 0, array $htmlAttribs = array())
	{
		if(func_num_args() == 0) {
			return $this;
		}
		else {
			return $this->webImage($path, $width, $height, $htmlAttribs);
		}
	}

	/**
	 * @param string $path
	 * @param integer|string $width
	 * @param integer|string $height
	 * @param string $type
	 * @return array
	 */
	public function info($path, $width = 0, $height = 0, $type = '')
    {
		$imgObj = t3lib_div::makeInstance('t3lib_stdGraphic');
		$imgObj->init();
		$imgObj->mayScaleUp = 0;
		$imageInfo = $imgObj->getImageDimensions($path);
    	if(is_array($imageInfo)) {
			$scaledImageInfo = $imgObj->imageMagickConvert($path, $type, $width, $height, '', '', '', 1);
			if(is_array($scaledImageInfo)) {
				// Source and scaled image have identical dimensions
				if($scaledImageInfo[0] === $imageInfo[0]) {
					if($scaledImageInfo[1] === $imageInfo[1]) {
						$scaledImageInfo = $imageInfo;
					}
				}
        		if($this->useAbsoluteUrl === true) {
        			$scaledImageInfo[3] = t3lib_div::getIndpEnv('TYPO3_SITE_URL') . $scaledImageInfo[3];
        		}
				return $scaledImageInfo;
			} else {
                trigger_error('ImageMagick is not installed', E_USER_NOTICE);
        		if($this->useAbsoluteUrl === true) {
        			$imageInfo[3] = t3lib_div::getIndpEnv('TYPO3_SITE_URL') . $imageInfo[3];
        		}
        		if(!empty($width) && !empty($height)) {
            		$imageInfo[0] = intval($width);
            		$imageInfo[1] = 0;
        		} elseif(!empty($width)) {
            		$imageInfo[0] = intval($width);
            		$imageInfo[1] = 0;
        		} elseif(!empty($height)) {
            		$imageInfo[0] = 0;
            		$imageInfo[1] = intval($height);
        		}
                return $imageInfo;
			}
		}
		return array();
	}

	/**
	 * Generates a cached image
	 *
	 * @param string $path
	 * @param integer|string $width
	 * @param integer|string $height
	 * @param array $htmlAttribs
	 * @return string The element XHTML.
	 */
    public function webImage($path, $width = 0, $height = 0, array $htmlAttribs = array())
    {
        $xhtml = '';
		$imageInfo = $this->info($path, $width, $height, 'WEB');
    	if(is_array($imageInfo) && !empty($imageInfo)) {
    	    $xhtml = $this->htmlTag($imageInfo, $htmlAttribs);
    	}
		return $xhtml;
	}

	/**
	 * Generates the image html tag
	 *
	 * @param array $imageInfo
	 * @param array $htmlAttribs
	 * @return string The element XHTML.
	 */
	public function htmlTag(array $imageInfo, array $htmlAttribs = array())
	{
		if(!isset($htmlAttribs['alt'])) {
			$htmlAttribs['alt'] = '';
		}

		if(!empty($imageInfo[0])) {
            $htmlAttribs['width']	= $imageInfo[0];
		}
		if(!empty($imageInfo[1])) {
            $htmlAttribs['height']	= $imageInfo[1];
		}
		$htmlAttribs['src']		= $imageInfo[3];

		return '<img'
				. $this->htmlAttribs($htmlAttribs)
				. ' />';
	}

	/**
	 * @param boolean $flag
	 * @return tx_zlib_viewhelper_image
	 */
	public function setUseAbsoluteUrl($flag)
	{
	    $this->useAbsoluteUrl = (bool)$flag;
	    return $this;
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_image.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_image.php']);
}
